/*
 * Copyright (c) 2024 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
 */

#include <stdio.h>
#include <zephyr/sys/printk.h>
#include <psa/crypto.h>
#include <psa/crypto_types.h>
#include <cracen_psa_kmu.h>


/* The key slots for MCUboot are identified as UROT_PUBKEY[n], where n is [0,2]
 * and each MCUboot key uses two slots for ED25519; the provided IDs are
 *  230-231 (gen 2)
 *  228-229 (gen 1)
 *  226-227 (gen 0)
 * and MCUboot key ID is identified by lower key id.
 * There are two 32 byte key slots used per key, even though 255 bits fit in
 * 32 bytes, because there is metadata also stored with the key and allocation
 * of space is only allowed by 32 byte slots.
 */
#define UROT_PUBKEY_2		230
#define UROT_PUBKEY_1		228
#define UROT_PUBKEY_0		226

#define MK_PSA_KEY_HANDLE(key) \
	PSA_KEY_HANDLE_FROM_CRACEN_KMU_SLOT(CRACEN_KMU_KEY_USAGE_SCHEME_RAW, key)

uint8_t signature_default[] = {
	0x2e, 0x89, 0x14, 0x67, 0x34, 0x61, 0x7c, 0x9f,
	0xbb, 0xa5, 0x65, 0x55, 0xb3, 0xb0, 0xfb, 0x2a,
	0x9c, 0x6e, 0xf6, 0xbf, 0x84, 0x2d, 0x10, 0x79,
	0x99, 0x08, 0xd3, 0x9f, 0xe0, 0xbb, 0x4a, 0x8a,
	0x9c, 0x3a, 0xc7, 0xb0, 0x24, 0xcb, 0xaa, 0xdb,
	0x99, 0xf3, 0x71, 0x2d, 0xe6, 0x6d, 0xb8, 0x52,
	0xa5, 0xa9, 0xc8, 0x06, 0xa6, 0xc3, 0xa9, 0x5b,
	0x54, 0x89, 0x9f, 0xe9, 0xe7, 0xc9, 0xb1, 0x02
};

uint8_t signature_1[] = {
	0xd3, 0xba, 0x03, 0x82, 0x4c, 0x86, 0x0d, 0xea,
	0xcd, 0xf2, 0x83, 0xef, 0x28, 0x1e, 0x69, 0x45,
	0x13, 0x19, 0x20, 0xb0, 0x09, 0x84, 0x14, 0x81,
	0x97, 0x9f, 0xb0, 0xa0, 0x9e, 0x98, 0x3b, 0x34,
	0xda, 0x21, 0xfa, 0x28, 0x4d, 0xdf, 0xd3, 0xee,
	0x8f, 0x26, 0x27, 0xf4, 0xcd, 0x01, 0xee, 0x48,
	0x7d, 0x4f, 0x83, 0xf3, 0x1c, 0x63, 0x08, 0x70,
	0xd0, 0xe0, 0xff, 0x13, 0x57, 0x05, 0xda, 0x0b
};

uint8_t signature_2[] = {
	0x30, 0x4e, 0x66, 0x48, 0x29, 0x31, 0xbb, 0xbb,
	0x2a, 0xa9, 0xd4, 0xb9, 0xc0, 0xd2, 0x2d, 0x93,
	0xec, 0x98, 0x93, 0xef, 0xf1, 0x5f, 0x99, 0xc9,
	0x66, 0x9a, 0xa3, 0x54, 0x27, 0x9d, 0x70, 0x62,
	0x74, 0xf8, 0xaa, 0x17, 0xe4, 0xfc, 0x74, 0xbb,
	0xcd, 0x1f, 0x20, 0xfb, 0x06, 0xee, 0x16, 0xb3,
	0xb4, 0x40, 0xc5, 0xcc, 0xaa, 0xfd, 0x0f, 0x26,
	0x78, 0x51, 0xa4, 0x91, 0x77, 0xc8, 0x25, 0x01
};

static const uint8_t test_message[] = {
	0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f,
	0x72, 0x6c, 0x64, 0x20, 0x50, 0x53, 0x41, 0x0a
};

static int test_verify_key(psa_key_id_t key, const uint8_t *signature,
		size_t signature_len, const uint8_t *message,
		size_t message_len)
{
	psa_status_t status = psa_verify_message(key, PSA_ALG_PURE_EDDSA, message, message_len,
				    signature, signature_len);

	if (status != PSA_SUCCESS) {
		printk("Signature verification failed %d\n", status);
	}
	return status;
}

int main(void)
{
	psa_status_t status = PSA_ERROR_BAD_STATE;

	status = psa_crypto_init();
	if (status != PSA_SUCCESS) {
		printk("PSA crypto init failed with error %d\n", status);
		return 0;
	}

	status = test_verify_key(MK_PSA_KEY_HANDLE(UROT_PUBKEY_0), signature_default,
			sizeof(signature_default), test_message, sizeof(test_message));
	if (status == PSA_SUCCESS) {
		printk("Default key verified\n");
	} else {
		printk("Default key failed\n");
	}

	status = test_verify_key(MK_PSA_KEY_HANDLE(UROT_PUBKEY_1), signature_1,
			sizeof(signature_1), test_message, sizeof(test_message));
	if (status == PSA_SUCCESS) {
		printk("Key 1 verified\n");
	} else {
		printk("Key 1 failed\n");
	}

	status = test_verify_key(MK_PSA_KEY_HANDLE(UROT_PUBKEY_2), signature_2,
			sizeof(signature_2), test_message, sizeof(test_message));
	if (status == PSA_SUCCESS) {
		printk("Key 2 verified\n");
	} else {
		printk("Key 2 failed\n");
	}

	return 0;
}
